<template>
  <div class="toolbar">
    <Button type="primary" @click="handleLeft">Name Left</Button>
    <Button type="primary" @click="handleRight">Sentence Right</Button>
    <Button type="primary" @click="handleReset">Reset</Button>
  </div>
  <VirtualizedTable
    ref="vtRef"
    :data-source="dataSource"
    :columns="columns"
    :width="1200"
    :height="480"
  />
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import VirtualizedTable from '@/components/virtualized'
import '@/components/virtualized/src/style/index.less'
import getData from '../data'
import { Button } from 'ant-design-vue'

const vtRef = ref()
const dataSource = ref<any[]>([])
const columns = ref<any[]>([])

const handleLeft = () => {
  const col = columns.value[1]
  col.fixed = 'left'
  columns.value.splice(1, 1)
  columns.value.unshift(col)
  vtRef.value.resetMeasuredIndices()
}

const handleRight = () => {
  const col = columns.value[2]
  col.fixed = 'right'
  columns.value.splice(2, 1)
  columns.value.push(col)
  vtRef.value.resetMeasuredIndices()
}

const handleReset = () => {
  columns.value[1].fixed = ''
  columns.value[2].fixed = ''
}

onMounted(() => {
  const res = getData()
  dataSource.value = res.dataSource
  columns.value = res.columns
  handleLeft()
  handleRight()
})
</script>

<style lang="less" scoped>
.toolbar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 12px;
  .ant-btn + .ant-btn {
    margin-left: 8px;
  }
}
</style>
